package token.mobileapp.controlactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import token.mobileapp.R;
import token.mobileapp.TokenActivity;
import token.mobileapp.model.UserInfo;
import token.mobileapp.persistenceactivity.UserInfoPersistence;
import token.mobileapp.utils.Constants;

/**
 * Created by IntelliJ IDEA.
 * UserInfo: AVAVT
 * Date: 7/9/11
 * Time: 6:16 AM
 * To change this template use File | Settings | File Templates.
 */
public class Login extends TokenActivity implements View.OnClickListener {

    private Button buttonLogin;
    private EditText editTextPINCode;
    private UserInfo userInfo;
    private int failedLogin = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        buttonLogin = (Button) findViewById(R.id.login_btn_login);
        buttonLogin.setOnClickListener(this);
        editTextPINCode = (EditText) findViewById(R.id.login_et_PIN);

        Intent intent = new Intent(this, UserInfoPersistence.class);
        startActivityForResult(intent, Constants.REQUEST_CODE_LOAD_USER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            userInfo = (UserInfo) bundle.getSerializable("userInfo");
        } else if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_CANCELED) {
            displayError(data);
        }
        if (requestCode == Constants.REQUEST_CODE_SAVE_USER && resultCode == Activity.RESULT_OK) {
            displayAlertThenFinish("You have entered incorrect PIN code 5 times in a row. Security Token has removed all account information and reset to its initial state. Please contact your service provider to reattach your accounts.");
            Intent intent = new Intent(this, Initialization.class);
            startActivity(intent);
        }
        else if (requestCode == Constants.REQUEST_CODE_SAVE_USER && resultCode == Activity.RESULT_CANCELED) {
            displayError(data);
        }
    }

    public void onClick(View v) {
        if (v.getId() == R.id.login_btn_login) {
            if (editTextPINCode.getText().toString().compareTo(userInfo.getPINCode()) == 0) {
                Intent intent = new Intent(this, MainMenu.class);
                startActivity(intent);
                this.finish();
            } else {
                failedLogin++;
                if(failedLogin < 5){
                    displayAlert("Incorrect PIN code. Please try again.");
                }
                else{
                    Intent intent = new Intent(this, UserInfoPersistence.class);
                    startActivityForResult(intent, Constants.REQUEST_CODE_SAVE_USER);
                }
            }
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Invalid request!")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
}
